﻿// ===============================================================================
// Project Name        :    Gfw.Repository.Admin
// Project Description :    
// ===============================================================================
// Class Name          :    UserRepository
// Namespace:          :    Gfw.Repository.Admin
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/22 11:39:03
// Update Time         :    2012/6/22 11:39:03
// ===============================================================================
// Copyright © ADICO-PC 2012 . All rights reserved.
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Admin;
using L.Basic.Authority.Models;
using NHibernate;

namespace Gfw.Repository.Admin
{
    public class SystemUserRepository : Repository<UsrSystemuser>
    {
        #region 业务逻辑
        /// <summary>
        /// 验证用户信息，并获取用户信息，包括部门和角色
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Validate(string loginName, string password, out User user)
        {
            user = null;
            var query = Session.CreateQuery(@"from UsrSystemuser as u 
                where u.LoginName=:loginName and u.Password=:password and u.IsLock=0")
                .SetString("loginName", loginName)
                .SetString("password", password);
            var users = query.List<UsrSystemuser>();
            if (users.Count == 1)
            {
                var systemUser = users.First();
                user = new User
                           {
                               Id = systemUser.Id,
                               LoginName = systemUser.LoginName,
                               Name = systemUser.Name,
                               Password = systemUser.Password,
                               State = UserState.Normal,
                               Department = new Department()
                               {
                                   Id = systemUser.Departmentid.Id,
                                   Name = systemUser.Departmentid.Name,
                                   Description = systemUser.Departmentid.Description
                               },
                               Roles = new List<Role>()
                           };
                foreach (var v in systemUser.UsrUserroleList)
                {
                    user.Roles.Add(new Role()
                                       {
                                           Id = v.Roleid.Id,
                                           Name = v.Roleid.Name,
                                           Description = v.Roleid.Description,
                                           Code = v.Roleid.Code
                                       });
                }


                return true;

            }
            return false;
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ChangePassword(string loginName, string oldPassword, string newPassword)
        {
            var query = Session.CreateQuery(@"from UsrSystemuser as u 
                where u.LoginName=:loginName and u.Password=:oldPassword")
                .SetString("loginName", loginName)
                .SetString("oldPassword", oldPassword);
            var user = query.List<UsrSystemuser>().FirstOrDefault();
            if (user != null)
            {
                user.Password = newPassword;
                base.Update(user);
                return true;
            }
            return false;
        }
        #endregion 业务逻辑

        #region 保存
        public bool RegisterCheck(UsrSystemuser user)
        {
            var query = Session.CreateQuery("select count(*) from UsrSystemuser u where u.LoginName=:loginName")
                .SetString("loginName", user.LoginName);
            if (query.UniqueResult<long>() == 0)
                return true;
            return false;

        }

        public bool Save(UsrSystemuser user, string departmentId)
        {
            var department = this.Session.Get<UsrDepartment>(departmentId);
            if (department == null)
                throw new ObjectNotFoundException("Save", "LpfDepartment");
            return Save(user, department);
        }
        public bool Save(UsrSystemuser user, UsrDepartment department)
        {

            user.Departmentid = department;
            if (RegisterCheck(user))
            {
                base.Save(user);
                return true;
            }
            return false;
        }
        #endregion

        #region 查询
        public IList<UsrSystemuser> GetUserByDepartment(string departmentId)
        {
            var department = this.Session.Get<UsrDepartment>(departmentId);
            if (department == null)
                throw new ObjectNotFoundException("GetUserByDepartment", "LpfDepartment");
            return department.UsrSystemuserList;
        }

        #endregion



    }

}
